﻿Imports System.ComponentModel
Imports System.Xml.XPath

<Serializable()> _
Public Class GameInfo
    Implements INotifyPropertyChanged


#Region "Members"
    Private m_MediaFiles As List(Of MediaFile)
    Private m_GlobalEvents As List(Of EventBase)
    Private m_startingMovieID As Integer
#End Region

#Region "Constructors"

    Public Sub New()
        m_MediaFiles = New List(Of MediaFile)
        m_GlobalEvents = New List(Of EventBase)
    End Sub

#End Region

#Region "Properties"

    Public Property StartingMovieID() As Integer
        Get
            Return m_startingMovieID
        End Get
        Set(ByVal value As Integer)
            m_startingMovieID = value
        End Set
    End Property

    Public Property MediaFiles() As List(Of MediaFile)
        Get
            Return m_MediaFiles
        End Get
        Set(ByVal value As List(Of MediaFile))
            m_MediaFiles = value
            NotifyPropertyChanged("MediaFiles")
        End Set
    End Property

    Public Property GlobalEvents() As List(Of EventBase)
        Get
            Return m_GlobalEvents
        End Get
        Set(ByVal value As List(Of EventBase))
            m_GlobalEvents = value
            NotifyPropertyChanged("GlobalElements")
        End Set
    End Property

#End Region

    Public Event PropertyChanged As PropertyChangedEventHandler _
      Implements INotifyPropertyChanged.PropertyChanged

    Private Sub NotifyPropertyChanged(ByVal info As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub

#Region "Xml Serialization"
    Private Const ID As String = "id"
    Private Const ELEMENT_TYPE As String = "elementType"
    Private Const TOP_LEFT As String = "locationTopLeft"
    Private Const BOTTOM_RIGHT As String = "locationBottomRight"
    Private Const COLOR As String = "color"
    Private Const TEXT As String = "text"
    Private Const IS_FLICKERING As String = "isFlickering"

    Private Const GAME_INFO As String = "GameInfo"
    Private Const HEIGHT As String = "screenHeight"
    Private Const WIDTH As String = "screenWidth"
    Private Const GLOBAL_ELEMENTS As String = "GlobalElements"
    Private Const ELEMENT As String = "Element"
    Private Const MOVIES As String = "Movies"

    Private Const MEDIA_FILES As String = "mediaFiles"
    Private Const MEDIA_FILE As String = "mediaFile"
    Private Const URL As String = "url"
    Private Const MEDIA_FILE_TYPE As String = "mediaFileType"

    Private Const STARTING_MOVIE_ID As String = "StartingMovieId"


#Region "Serialize"
    Public Function SerializeToXML(ByVal p_Path As String) As Boolean
        Dim l_XElement = getXMLString()

        Dim l_xDeclaration = New XDeclaration(1.0, "UTF-8", "yes")
        Dim l_xDoc As New XDocument(l_xDeclaration, l_XElement)



        'l_xDoc.Add(l_XElement)
        '    l_xDoc.AddFirst(l_XElement)


        'IO.File.WriteAllText("C:\123\Game.xml", l_xDoc.ToString)
        l_xDoc.Save(p_Path)
    End Function

    Public Function getXMLString() As XElement
        Dim l_XElement As New XElement(GAME_INFO)
        l_XElement.Add(New XAttribute(HEIGHT, "260"))
        l_XElement.Add(New XAttribute(WIDTH, "470"))

        'MEDIA_FILES
        Dim l_xMediaFiles As New XElement(MEDIA_FILES)
        l_XElement.Add(l_xMediaFiles)
        For Each l_MediaFile In Me.MediaFiles
            Dim l_xMediaFile As New XElement(MEDIA_FILE)
            l_xMediaFile.Add(New XAttribute(ID, l_MediaFile.ID))
            l_xMediaFile.Add(New XAttribute(URL, l_MediaFile.Url))
            l_xMediaFile.Add(New XAttribute(MEDIA_FILE_TYPE, l_MediaFile.Type))
            l_xMediaFiles.Add(l_xMediaFile)
        Next

        Dim l_xStartingMovieID As New XElement(STARTING_MOVIE_ID)
        l_XElement.Add(l_xStartingMovieID)
        l_xStartingMovieID.Add(m_startingMovieID)

        'GLOBAL ELEMENTS!
        Dim l_XGlobalElements As New XElement(GLOBAL_ELEMENTS)
        l_XElement.Add(l_XGlobalElements)

        For Each l_GlobalEvent As EventBase In Me.GlobalEvents
            Dim l_XGlobalElement As New XElement(ELEMENT)
            l_XGlobalElement.Add(New XAttribute(ID, l_GlobalEvent.EventID))
            l_XGlobalElement.Add(New XAttribute(ELEMENT_TYPE, GetUIElementType(l_GlobalEvent.EventType)))
            l_XGlobalElement.Add(New XAttribute(TOP_LEFT, l_GlobalEvent.EventArea.TopLeft))
            l_XGlobalElement.Add(New XAttribute(BOTTOM_RIGHT, l_GlobalEvent.EventArea.BottomRight))
            l_XGlobalElement.Add(New XAttribute(COLOR, GetRGBA(l_GlobalEvent.BackgroundColor)))
            l_XGlobalElement.Add(New XAttribute(TEXT, l_GlobalEvent.Text))
            l_XGlobalElement.Add(New XAttribute(IS_FLICKERING, l_GlobalEvent.IsFlickering))


            l_XGlobalElements.Add(l_XGlobalElement)
        Next

        Dim l_XMovies As New XElement(MOVIES)
        l_XElement.Add(l_XMovies)
        For Each l_MediaFile In Me.MediaFiles
            If l_MediaFile.Type = MediaFileType.Movie Then
                Dim l_movie = CType(l_MediaFile, Movie)
                'get the movies xelement
                l_XMovies.Add(l_movie.getXMLString(m_MediaFiles))
            End If

        Next
        
        Return l_XElement

    End Function

    Public Shared Function GetUIElementType(ByVal p_eventType As EventsType) As UiElement
        If p_eventType = EventsType.ButtonClick Then
            Return UiElement.button
        ElseIf p_eventType = EventsType.Text Then
            Return UiElement.text
        ElseIf p_eventType = EventsType.HotSpot Then
            Return UiElement.none
        ElseIf p_eventType = EventsType.PictureBox Then
            Return UiElement.pictureBox
        ElseIf p_eventType = EventsType.MovablePicture Then
            Return UiElement.pictureBox

        End If
    End Function

    Public Shared Function GetRGBA(ByVal p_color As System.Windows.Media.Color) As String
        Dim l_string = p_color.R & "," & p_color.G & "," & p_color.B & "," & p_color.A
        Return l_string
    End Function

#End Region

#Region "deserialize"
    Public Sub DesirializeFromXML(ByVal l_xelement As XElement)

        Dim l_xElementGameInfo As XElement = l_xelement

        If l_xElementGameInfo.Name = GAME_INFO Then

            For Each l_element In l_xElementGameInfo.Elements
                If l_element.Name = MOVIES Then
                    For Each l_MovieElement In l_element.Elements
                        Dim l_movieID = l_MovieElement.Attribute(ID).Value
                        Dim l_movie As Movie = (From l_mov In m_MediaFiles Where l_mov.ID = l_movieID Select l_mov).SingleOrDefault
                        l_movie.XMLDeserialize(l_MovieElement, MediaFiles)
                    Next
                ElseIf l_element.Name = GLOBAL_ELEMENTS Then
                    For Each l_xGlobalElement In l_element.Elements
                        Dim l_event As New EventBase
                        l_event.DeserializeGlobalEventXml(l_xGlobalElement)
                        Me.GlobalEvents.Add(l_event)
                    Next
                ElseIf l_element.Name = STARTING_MOVIE_ID Then
                    Me.StartingMovieID = l_element.Value
                End If

            Next

            Dim l_startingMovie = (From l_file In MediaFiles Where l_file.ID = StartingMovieID Select l_file).SingleOrDefault
            If l_startingMovie IsNot Nothing Then
                l_startingMovie.IsStartingMovie = True
            End If
        End If


    End Sub
#End Region




#End Region


End Class
